﻿Imports System.Data.OleDb
Module LT_BANG
    Public Chuoi_ket_noi As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Data\QLBaoCao.accdb"
    Public Function Doc(ByVal Chuoi_lenh As String) As DataTable
        Dim Kq As New DataTable
        ' Khai báo bộ thích ứng 
        Try
            Dim Bo_thich_ung As New OleDbDataAdapter(Chuoi_lenh, Chuoi_ket_noi)
            ' Dùng Bộ thích ứng đọc dữ liệu 
            Bo_thich_ung.FillSchema(Kq, SchemaType.Source)
            Bo_thich_ung.Fill(Kq)
        Catch Loi As Exception
            Dim Thong_bao_loi As String = "Lỗi truy xuất CSDL " & vbCrLf
            Thong_bao_loi &= "Chuỗi lệnh :" & Chuoi_lenh & vbCrLf
            Thong_bao_loi &= "Chuỗi kết nối :" & Chuoi_ket_noi & vbCrLf
            Thong_bao_loi &= "Lỗi :" & Loi.Message & vbCrLf
            MessageBox.Show(Thong_bao_loi)
        End Try
        Return Kq
    End Function
    Public Function Doc_cau_truc(ByVal Ten_bang As String) As DataTable
        Dim Kq As New DataTable
        Dim Chuoi_lenh As String = "Select * From " & Ten_bang
        ' Khai báo bộ thích ứng 
        Dim Bo_thich_ung As New OleDbDataAdapter(Chuoi_lenh, Chuoi_ket_noi)
        ' Dùng Bộ thích ứng đọc dữ liệu 
        Bo_thich_ung.FillSchema(Kq, SchemaType.Source)
        Return Kq
    End Function
    Public Function Ghi(ByVal Bang As DataTable, ByVal Ten_bang As String) As Integer
        Dim Kq As Integer = 0
        Dim Chuoi_lenh As String = "Select * From " & Ten_bang
        Dim Bo_thich_ung As New OleDbDataAdapter(Chuoi_lenh, Chuoi_ket_noi)
        Dim Bo_phat_sinh_lenh As New OleDbCommandBuilder(Bo_thich_ung)
        Kq = Bo_thich_ung.Update(Bang)
        Return Kq
    End Function
    
    Public Sub Fillcombo(ByVal Chuoi_lenh As String, ByVal cbo As ComboBox, ByVal ma As String, ByVal ten As String)
        Dim table As New DataTable
        table = Doc(Chuoi_lenh)
        If (table.Rows.Count > 0) Then
            cbo.DataSource = table
            cbo.ValueMember = ma
            cbo.DisplayMember = ten
        End If
    End Sub
    
    Public Function Chuyensosangchu(ByVal sNumber As String) As String
        Dim mLen As Long, i As Long
        Dim mDigit As Long
        Dim mTemp As String = ""
        Dim mNumText() As String
        sNumber = Replace(sNumber, ",", "")
        mNumText = Split(" không; một; hai; ba; bốn; năm; sáu; bảy; tám; chín", ";")
        mLen = Len(sNumber)
        For i = 1 To mLen
            mDigit = Mid(sNumber, i, 1)
            mTemp = mTemp & "" & mNumText(mDigit)
            If (mLen = i) Then
                Exit For
            End If
            Select Case (mLen - i) Mod 9
                Case 0
                    mTemp = mTemp & " tỷ"
                    If Mid(sNumber, i + 1, 3) = "000" Then i = i + 3
                    If Mid(sNumber, i + 1, 3) = "000" Then i = i + 3
                    If Mid(sNumber, i + 1, 3) = "000" Then i = i + 3
                Case 6
                    mTemp = mTemp & " triệu"
                    If Mid(sNumber, i + 1, 3) = "000" Then i = i + 3
                    If Mid(sNumber, i + 1, 3) = "000" Then i = i + 3
                Case 3
                    mTemp = mTemp & " nghìn"
                    If Mid(sNumber, i + 1, 3) = "000" Then i = i + 3
                Case Else
                    Select Case (mLen - i) Mod 3
                        Case 2
                            mTemp = mTemp & " trăm"
                        Case 1
                            mTemp = mTemp & " mươi"
                    End Select
            End Select
        Next
        mTemp = Replace(mTemp, " không mươi không", "")
        mTemp = Replace(mTemp, " không mươi không", "")
        mTemp = Replace(mTemp, " không mươi", " lẻ")
        mTemp = Replace(mTemp, " mươi không", " mươi")
        mTemp = Replace(mTemp, " một mươi", " mười")
        mTemp = Replace(mTemp, " mươi bốn", " mươi tư")
        mTemp = Replace(mTemp, " lẻ bốn", " lẻ tư")
        mTemp = Replace(mTemp, " mươi lăm", " mươi lăm")
        mTemp = Replace(mTemp, " mươi một", " mươi mốt")
        mTemp = Replace(mTemp, " mười năm", " mười năm")
        mTemp = Trim(mTemp)
        Return UCase(Left(mTemp, 1)) & Mid(mTemp, 2) & " đồng"
    End Function
End Module
